<template>
  <div class="register-container">
    <transition name="el-fade-in-linear">
      <el-form v-show="show" ref="resetForm" :model="resetForm" :rules="registerRules" class="register-form" auto-complete="on" label-position="left">
        <div class="title-container">
          <h3 class="title">重置密码</h3>
        </div>
        <el-steps :active="step" finish-status="success" align-center style="margin-bottom:25px;">
          <el-step title="确认账号" />
          <el-step title="重置密码" />
          <el-step title="重置成功" />
        </el-steps>
        <el-form-item v-show="step1visible" prop="username">
          <span class="svg-container">
            <svg-icon icon-class="user" />
          </span>
          <el-input
            ref="username"
            v-model="resetForm.info"
            placeholder="请输入注册时的邮箱、手机号或用户名"
            name="username"
            type="text"
            tabindex="1"
            auto-complete="on"
          />
        </el-form-item>

        <el-form-item v-show="step2visible" prop="password">
          <span class="svg-container">
            <svg-icon icon-class="password" />
          </span>
          <el-input
            :key="passwordType"
            ref="password"
            v-model="resetForm.password"
            :type="passwordType"
            placeholder="密码"
            name="password"
            tabindex="2"
            @keyup.enter.native="handleRegister"
          />
          <span class="show-pwd" @click="showPwd">
            <svg-icon :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'" />
          </span>
        </el-form-item>
        <div v-show="step3visible" style="display:flex;">
          <img src="../../assets/checked.png" style="height:150px; width:150px;">
          <h2 style="color:white;">重置完成，返回首页登录吧</h2>
        </div>
        <el-form-item v-show="step2visible" prop="confirmpassword">
          <span class="svg-container">
            <svg-icon icon-class="password" />
          </span>
          <el-input
            :key="passwordType"
            ref="password"
            v-model="resetForm.confirmpassword"
            :type="passwordType"
            placeholder="确认密码"
            name="password"
            tabindex="2"
            @keyup.enter.native="handleRegister"
          />
        </el-form-item>
        <el-row>
          <el-button :disabled="perviousDisable" :loading="loading" style="width:45%;float:left;" type="primary" @click="pervious">
            上一步
          </el-button>
          <el-button :disabled="nextDisable" :loading="loading" style="width:45%;float:right;" type="primary" @click.native.prevent="handleRegister">
            下一步
          </el-button>
        </el-row>
        <el-link type="primary" style="float:right;margin-top:15px;" @click="$router.go(-1)">已有账号？立刻登录</el-link>
      </el-form>

    </transition>
  </div>
</template>

<script>
import { validUsername } from '@/utils/validate'
export default {
  name: 'Register',
  data() {
    const validateUsername = (rule, value, callback) => {
      if (!validUsername(value)) {
        callback(new Error('用户名格式不正确'))
      } else {
        callback()
      }
    }
    const validatePassword = (rule, value, callback) => {
      if (value.length < 6) {
        callback(new Error('密码不得小于六位'))
      } else if (this.resetForm.password !== this.resetForm.confirmpassword) {
        callback(new Error('两次输入的密码不一致'))
      } else {
        callback()
      }
    }
    return {
      step1visible: true,
      step2visible: false,
      step3visible: false,
      perviousDisable: true,
      nextDisable: false,
      passflag: false,
      step: 0,
      show: false,
      resetForm: {
        info: '',
        password: '',
        confirmpassword: ''
      },
      registerRules: {
        info: [{ required: true, trigger: 'blur', validator: validateUsername }],
        password: [{ required: true, trigger: 'blur', validator: validatePassword }]
      },
      loading: false,
      passwordType: 'password',
      redirect: undefined
    }
  },
  watch: {
    $route: {
      handler: function(route) {
        this.redirect = route.query && route.query.redirect
      },
      immediate: true
    }
  },
  mounted: function() {
    this.doAnime()
  },
  methods: {
    doAnime: function() {
      this.show = true
    },
    showPwd() {
      if (this.passwordType === 'password') {
        this.passwordType = ''
      } else {
        this.passwordType = 'password'
      }
      this.$nextTick(() => {
        this.$refs.password.focus()
      })
    },
    pervious() {
      this.step--
      if (this.step > 0) {
        this.perviousDisable = false
      } else {
        this.perviousDisable = true
      }
      this.step1visible = false
      this.step2visible = false
      this.step3visible = false
      if (this.step === 1) {
        this.step2visible = true
      } else {
        this.step1visible = true
      }
    },
    next() {
      this.step++
      this.perviousDisable = false
      this.step1visible = false
      this.step2visible = false
      this.step3visible = false
      if (this.step === 1) {
        this.step2visible = true
      } else {
        this.step3visible = true
        this.perviousDisable = true
      }
    },
    handleRegister() {
      switch (this.step) {
        case 0:
          if (!this.resetForm.info || String(this.resetForm.info).trim() === '') {
            this.$message({
              message: '请输入信息',
              type: 'error'
            })
            return
          }
          this.$store.dispatch('user/exists', this.resetForm).then(() => {
            this.loading = false
            this.passflag = true
            this.$message({
              message: '查询成功',
              type: 'success'
            })
            this.next()
          }).catch(() => {
            this.loading = false
            this.passflag = false
          })
          return
        case 1:
          this.$refs.resetForm.validate(valid => {
            if (!valid) {
              this.$message({
                message: '两次输入的密码不一致',
                type: 'error'
              })
            } else {
              this.loading = true
              this.$store.dispatch('user/resetpwd', this.resetForm).then(() => {
                this.next()
                this.loading = false
              }).catch(() => {
                this.loading = false
              })
            }
          })
          break
        case 2:
          this.$router.push({ path: '/login' })
          break
      }
    }
  }
}
</script>

<style lang="scss">
/* 修复input 背景不协调 和光标变色 */
/* Detail see https://github.com/PanJiaChen/vue-element-admin/pull/927 */

$bg:#283443;
$light_gray:#fff;
$cursor: #fff;

@supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
  .register-container .el-input input {
    color: $cursor;
  }
}

/* reset element-ui css */
.el-step__title.is-process {
  color: #3a8ee6
}
.el-step__icon-inner{
  color: #3a8ee6
}
.register-container {
  .el-input {
    display: inline-block;
    height: 47px;
    width: 85%;

    input {
      background: transparent;
      border: 0px;
      -webkit-appearance: none;
      border-radius: 0px;
      padding: 12px 5px 12px 15px;
      color: $light_gray;
      height: 47px;
      caret-color: $cursor;

      &:-webkit-autofill {
        box-shadow: 0 0 0px 1000px $bg inset !important;
        -webkit-text-fill-color: $cursor !important;
      }
    }
  }

  .el-form-item {
    border: 1px solid rgba(255, 255, 255, 0.1);
    background: rgba(0, 0, 0, 0.1);
    border-radius: 5px;
    color: #454545;
  }
}
</style>

<style lang="scss" scoped>
$bg:#2d3a4b;
$dark_gray:#889aa4;
$light_gray:#eee;

.register-container {
  min-height: 100%;
  width: 100%;
  background-color: $bg;
  overflow: hidden;

  .register-form {
    position: relative;
    width: 520px;
    max-width: 100%;
    padding: 160px 35px 0;
    margin: 0 auto;
    overflow: hidden;
  }

  .tips {
    font-size: 14px;
    color: #fff;
    margin-bottom: 10px;

    span {
      &:first-of-type {
        margin-right: 16px;
      }
    }
  }

  .svg-container {
    padding: 6px 5px 6px 15px;
    color: $dark_gray;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
  }

  .title-container {
    position: relative;

    .title {
      font-size: 26px;
      color: $light_gray;
      margin: 0px auto 40px auto;
      text-align: center;
      font-weight: bold;
    }
  }

  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }
}
</style>
